package com.fourth_groups.shopping121store.ui.cart.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fourth_groups.shopping121store.R;
import com.fourth_groups.shopping121store.bean.GoodsHotSaleBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/12.
 */

public class CartHotSaleAdapter extends BaseAdapter {

    private List<GoodsHotSaleBean.DataBean> goodsHotSaleBeens = new ArrayList<>();


    public void setData(List<GoodsHotSaleBean.DataBean> mGoodsHotSaleBeens) {//设置数据
        this.goodsHotSaleBeens = mGoodsHotSaleBeens;
        notifyDataSetChanged();
    }

    public CartHotSaleAdapter() {};

    @Override
    public int getCount() {
        return goodsHotSaleBeens != null ? goodsHotSaleBeens.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return goodsHotSaleBeens != null ? goodsHotSaleBeens.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_good_common, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //绑定数据

        String img = goodsHotSaleBeens.get(position).getImg();

        String name = goodsHotSaleBeens.get(position).getName();
        String price = goodsHotSaleBeens.get(position).getPrice();
        String market_price = goodsHotSaleBeens.get(position).getMarket_price();
        String fencheng = goodsHotSaleBeens.get(position).getFencheng();
        Log.i("TAG", "####" + name);

        //使用glide下载图片
        Glide.with(parent.getContext()).load(img).into(holder.goodImage);

        holder.goodName.setText(name);
        holder.goodPrice.setText(price);
        holder.goodMarketPrice.setText(market_price);
        holder.goodFencheng.setText(fencheng);

        return convertView;
    }

    static class ViewHolder {
        private ImageView goodImage;
        private TextView goodName, goodPrice, goodMarketPrice, goodFencheng;

        public ViewHolder(View contentView) {
            goodImage = (ImageView) contentView.findViewById(R.id.iv_good_img_common);
            goodName = (TextView) contentView.findViewById(R.id.tv_good_name_common);
            goodPrice = (TextView) contentView.findViewById(R.id.tv_good_price_common);
            goodMarketPrice = (TextView) contentView.findViewById(R.id.tv_good_market_price_common);
            goodFencheng = (TextView) contentView.findViewById(R.id.tv_good_fencheng_common);
        }
    }

}
